#include <myheader.h>

/*Usage:  */
int main(int argc, char *argv[])
{
    const char *data = "i am a son";
    int pipefd[2];
    pipe(pipefd);
    if(fork() == 0){
        //子进程写
        int cnt = 0;
        close(pipefd[0]);
        while(1){
            //一直写数据
         int ret = write(pipefd[1],data,strlen(data));
         sleep(2);
         printf("i am in a loop\n");
         ERROR_CHECK(ret,-1,"write");
         cnt++;
         if(cnt == 5){
             close(pipefd[1]);
             printf("close write port\n");
         }
        }
        printf("i am out of the loop\n");
    }else{
        //父进程读
        close(pipefd[1]);
        char buf[60] = {0};
        int cnt = 0;
        while(1){
            memset(buf,0,sizeof(buf));
            //一直读数据
            int ret = read(pipefd[0],buf,sizeof(buf));
            printf("%d\n",ret);
            ERROR_CHECK(ret,-1,"read");
            printf("%s\n",buf);
        }
        wait(NULL);

    }
    return 0;
}

